﻿// Copyright (C) Microsoft Corporation. All Rights Reserved.
// This code released under the terms of the
// Microsoft Limited Public License (Ms-LPL).

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;

using Microsoft.VisualStudio.IntelliTrace;
using Microsoft.VisualStudio.Samples.IHandlers;

namespace Microsoft.VisualStudio.Samples
{
	internal class CheckpointChainFormatter: EventChainFormatter
	{
		public CheckpointChainFormatter(IntelliTraceProcess process)
			: base(process)
		{
            handlers = new List<CheckpointChainHandler>();
		}

        IList<CheckpointChainHandler> handlers;

        public override void FireEvents()
        {
            if (handlers.Count > 0) base.FireEvents();
        }

		
		protected override void FireEvent(IntelliTraceEvent ev)
		{
            foreach (CheckpointChainHandler handler in handlers)
            {
                if (ev.GetType() == typeof(ThreadCheckpointEvent) || ev.GetType().IsSubclassOf(typeof(ThreadCheckpointEvent)))
                {
                    handler.ThreadCheckpointEvent(ev);
                }
                else if (ev.GetType() == typeof(DiagnosticCheckpointEvent) || ev.GetType().IsSubclassOf(typeof(DiagnosticCheckpointEvent)))
                {
                    handler.DiagnosticCheckpointEvent(ev);
                }
            }
		}

        protected override StreamChain getStreamChain()
        {
            return Process.CreateProcessChain<CheckpointStreamChain>();
        }


        public void AddHandler(CheckpointChainHandler handler)
        {
            handlers.Add(handler);
        }
    }
}
